package com.huajx.client.test;

import com.huajx.armp.htxny.codec.HtxnyDecoder;
import com.huajx.armp.htxny.codec.HtxnyEncoder;
import com.huajx.core.util.MessageKit;
import com.huajx.util.ByteUtil;
import com.huajx.util.CharUtil;
import com.huajx.util.HexUtil;
import com.huajx.util.StrUtil;
import com.huajx.util.date.DateUtil;
import org.apache.mina.core.future.ConnectFuture;
import org.apache.mina.core.service.IoConnector;
import org.apache.mina.core.service.IoHandlerAdapter;
import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.codec.ProtocolCodecFactory;
import org.apache.mina.filter.codec.ProtocolCodecFilter;
import org.apache.mina.filter.codec.ProtocolDecoder;
import org.apache.mina.filter.codec.ProtocolEncoder;
import org.apache.mina.transport.socket.nio.NioSocketConnector;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.net.InetSocketAddress;
import java.util.Date;

/**
 * @program: di-parent
 * @description:
 * @author: lisa 叶新华
 * @create: 2020-03-25 16:46
 */
public class HtxnyClient extends IoHandlerAdapter {
    private static final Logger logger = LoggerFactory.getLogger(HtxnyClient.class);
    private IoConnector connector;
    private static IoSession session;
//    private final static String IP = "106.14.20.39";
//    private final static int PORT = 9230;
    private final static String IP = "127.0.0.1";
//    private final static String IP = "47.101.48.164";
    private final static int PORT = 11001;

    public static void main(String[] args) throws InterruptedException {
        HtxnyClient client = new HtxnyClient();
        //弋阳县方志敏中学 电话请求
        String command = "232305FE3231353530304C32433033303030303030010029130C1A0B1A050001653937633137323163383533366337343934666530663161643435366265366101F2";

        String schoolId = "361102030";
        String cardId = "12345678910";

        client.sent(command);

//        String dateReport = "232302FE544553543556494E353030375330303031010066130C190F3403010103030000000013BA0D16271755010F270F0000020101044F4E204E1F4D0D20271004010356002F050106D0B89C015FAFDF0601010FAA01010FAA01FFFF01FFFF070000000000000000000901010001FF0801010D16271700010001010FAA81";
        String dateReport = "232302fe4c43465a31465042344a305a303035373301015914031e0d1f1e01020101000000032cfc07ab230b42020050c9000002010103384e204e2046000027100500071f919001dee7960601070d47013b000001034101013c0700000000000000000009010100143c40414140403c4041413c40414140403c40414108010107ab230b00740001740d3d0d3e0d450d3b0d410d3e0d470d360d350d350d3b0d360d3a0d3a0d3c0d3a0d3a0d370d380d3b0d3a0d310d370d390d330d320d340d360d3d0d3c0d330d330d310d360d380d390d3e0d3e0d450d3b0d410d3e0d470d360d350d350d3b0d360d3a0d3a0d3b0d3a0d3a0d370d380d3b0d3a0d31000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000012";

        client.sent(dateReport);

//        for (int i = 0; i < 10; i++) {
//            logger.info("线程{}", i);
//            Thread thread = new Thread(()->{
//                client.sent(command);
//                client.sent(dateReport);
////                for (int j = 0; j < 10; j++) {
////                    logger.info("传送数据{}", j);
////                    client.sent(command);
////                }
//                try {
//                    Thread.sleep(100);
//                } catch (InterruptedException e) {
//                    e.printStackTrace();
//                }
//            });
//            thread.start();
//        }

        //弋阳县方志敏中学 电话请求
//        command="TPJ"+"0001"+"0"+"361100000000000001"+"0016"+"01"+"3047220073          9D";
//        client.sent(command);

        Thread.sleep(1000);
        session.closeNow();
    }


    public void sent(String commond) {
        session.write(HexUtil.hexToBytes(commond));
    }

    public HtxnyClient() {
        connector = new NioSocketConnector();
        connector.getFilterChain().addLast("codec",
                new ProtocolCodecFilter(new ProtocolCodecFactory() {
                    @Override
                    public ProtocolEncoder getEncoder(IoSession session) throws Exception {
                        return new HtxnyEncoder();
                    }

                    @Override
                    public ProtocolDecoder getDecoder(IoSession session) throws Exception {
                        return new HtxnyDecoder();
                    }
                })
        );
        connector.setHandler(this);
        ConnectFuture connFuture = connector.connect(new InetSocketAddress(IP, PORT));
        connFuture.awaitUninterruptibly();
        session = connFuture.getSession();
    }

    @Override
    public void messageReceived(IoSession iosession, Object message) throws Exception {
        super.messageReceived(iosession, message);
        if (message instanceof byte[]) {
            logger.info("接收数据: {}", HexUtil.bytesToHex((byte[]) message));
        }
    }

    @Override
    public void exceptionCaught(IoSession session, Throwable cause) throws Exception {
        super.exceptionCaught(session, cause);
        logger.info("客户端异常");
    }

    @Override
    public void messageSent(IoSession iosession, Object message) throws Exception {
        if (message instanceof byte[]) {
            logger.info("发送命令: {}", HexUtil.bytesToHex((byte[]) message));
        }
        super.messageSent(iosession, message);
    }

    @Override
    public void sessionClosed(IoSession iosession) throws Exception {
        logger.info("客户端会话关闭");
        super.sessionClosed(iosession);
        connector.dispose();
    }

    @Override
    public void sessionCreated(IoSession iosession) throws Exception {
        logger.info("客户端会话创建");
        super.sessionCreated(iosession);
    }

    @Override
    public void sessionIdle(IoSession iosession, IdleStatus idlestatus)
            throws Exception {
        logger.info("客户端会话休眠");
        super.sessionIdle(iosession, idlestatus);
    }

    @Override
    public void sessionOpened(IoSession iosession) throws Exception {
        logger.info("客户端会话打开");
        super.sessionOpened(iosession);
    }
}